<?php

namespace app\api\server;

use app\api\server\Goods as ServerGoods;
use app\common\controller\Server;
use app\common\library\Auth;
use app\common\model\Cart as ModelCart;
use app\common\model\goods\Goods;
use app\common\model\User;

/**
 * 购物车逻辑
 */
class Cart
{


    /**
     * 添加购物车
     *
     */
    public static function addCart($data, $type)
    {
        extract($data);
        if (!isset($num) || !$num) {
            $num = 1;
        }
        $auth = Auth::instance();
        if ($auth->state == 0) {
            exception('账户已冻结，禁止交易');
        }
        $goods = Goods::get($goods_id);
        if (!isset($goods) || !$goods) {
            exception('商品不存在');
        }
        $cart = ModelCart::get(['user_id' => $auth->id, 'class' => $class, 'goods_id' => $goods_id, 'spec_id' => $spec_id]);
        if ($cart) {
            switch ($add_sub) {
                case 0:
                    $cart->num = $num;
                    $cart->save();
                    break;
                case 1:
                    if ($type == 2) {
                        $cart->num = $num;
                        $cart->save();
                    } else {
                        $cart->setInc('num', $num);
                    }
                    break;
                case 2:
                    $cart->setDec('num', $num);
                    break;
            }
            if ($cart->num <= 0) {
                $cart->delete();
            }
        } else {
            $data = [
                'user_id' => $auth->id,
                'goods_id' => $goods_id,
                'spec_id' => $spec_id ?? 0,
                'num' => $num,
                'type' => $type,
                'class' => $class,
            ];
            $cart = ModelCart::create($data, true);
        }
        return $cart->id;
    }
    public static function lists($uid, $class)
    {

        $lists = ModelCart::with(['goods', 'spec'])->where('user_id', $uid)->where('class', $class)->paginate();
        foreach ($lists as $v) {

            $v->fee_array = ServerGoods::vip_price($v->goods, $v->spec, $v->num);
        }
        return $lists;
    }

    //获取购物车下单商品信息
    public static function getGoods($ids, $user)
    {

        $cart_goods = ModelCart::with(['goods', 'spec'])->where('user_id', $user['id'])->whereIn('id', $ids)->select();

        foreach ($cart_goods as $v) {
            if (!isset($v->goods) || !$v->goods) {
                exception('商品不存在，无法下单');
            }
            if (!isset($v->spec) || !$v->spec) {
                exception('商品规格不存在，无法下单');
            }
            if ($v->spec->stock < $v->num) {
                exception('商品【' . $v->goods->title . '】库存不足，无法下单');
            }
            if ($v->goods->deletetime != null) {
                exception('商品【' . $v->goods->title . '】已删除，无法下单');
            }
            $fee_array = ServerGoods::vip_price($v->goods, $v->spec, $v->num);
            $v->discount_amount = $fee_array['discount_amount'];
            $v->pay_fee = $fee_array['pay_fee'];
            $v->total_fee = $fee_array['total_fee'];
            $v->vip_price = $fee_array['vip_price'];
        }
        return $cart_goods;
    }
}
